package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.ASRecord;
import com.vehicle.pojo.Car;
import com.vehicle.service.ASRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Controller
@RequestMapping(value = "/asRecord")
public class ASRecordController extends BaseController {
    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ASRecordService asRecordService;
    @ResponseBody
    @RequestMapping("/asRecords")
    public DataGridView accidentRecordList(ASRecord asRecord, Integer carid){
        Car car = new Car();
        car.setCarid(carid);
        asRecord.setCar(car);
        return asRecordService.findASRecordPage(asRecord);
    }
    @ResponseBody
    @RequestMapping(value = "/deleteASRecord")
    public ResultDto deleteASRcord(Integer asid){
        ResultDto resultDto = new ResultDto();
        try {
            asRecordService.deleteASRecordByid(asid);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
        }
        return resultDto;
    }
    //生成年检序列码
    @ResponseBody
    @RequestMapping(value = "/as_serial")
    public String INSCodeBuilder(){
        char temp []={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
                'o','p','q','r','s','t','u','v','w','x','y','z',
                '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G',
                'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        String a="";
        Random random=new Random();
        for (int i=0;i<4;i++){
            a+=temp[random.nextInt(62)];
        }
        String code = new SimpleDateFormat("yyyyMMddHH").format(new Date());
        code+=a;
        return "INS"+code;
        }
    @ResponseBody
    @RequestMapping(value = "/addASRecord")
    public ResultDto addASRecord(ASRecord asRecord){
        ResultDto resultDto = new ResultDto();
        try {
            asRecordService.addASRecord(asRecord);
            resultDto.setMsg("增加成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("增加失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    @RequestMapping(value = "/updateASRecord")
    @ResponseBody
    public ResultDto updateASRecord(ASRecord asRecord){
        ResultDto resultDto = new ResultDto();
        try {
            asRecordService.updateASRecord(asRecord);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/deleteBatchASRecord")
    public ResultDto deleteBatchASRecord(@RequestParam("asIds[]") Integer [] asIds){
        ResultDto resultDto = new ResultDto();
        try {
            asRecordService.deleteBatchASRecord(asIds);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
    @ResponseBody
    @RequestMapping("/ip")
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ip != null && ip.length() > 15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }
}
